#-*- coding: UTF-8 -*-

import urllib.request as request
import sys


config = {
    "local_path": "./",
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Connection': 'keep-alive'
}

def get(url):
    req = request.Request(url, headers=headers)
    f = request.urlopen(req)
    data = f.read()
    return data


def download_cbk(count, block_size, total_size):
    print(count, block_size, total_size)
    # percent = count * block_size * 100.0 / total_size
    # sys.stdout.write(u"\r process : %.2f%%" % percent + ' complete\n')
    sys.stdout.write(u"\r %d/%d" % (block_size * count , total_size) +"\n")
    sys.stdout.flush()


def download(url):
    file_name = url.rsplit('/', 1)
    request.urlretrieve(url, config['local_path'] + file_name[1], reporthook=download_cbk)


if __name__ == "__main__":
    print("test for download()")
    download("http://www.baidu.com")
    print("test for get()")
    print(len(get("http://www.baidu.com")))
